<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力会务会议发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  All rights reserved.
// +----------------------------------------------------------------------
// | 沃德会务会议系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.maiwd.cn
// +----------------------------------------------------------------------
/**
 * Class UserWechat
 * Desc  微信用户控制器
 * Create on 2024/3/16 9:14
 * Create by wangyafang
 */

namespace app\api\controller\wddj;


use app\common\controller\Api;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use Exception;

class UserWechat extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\wddj\UserWechat();
    }



    /**
     * Desc  更新昵称和头像
     * Create on 2024/3/28 14:12
     * Create by wangyafang
     */
    public function update_nickname_avatar()
    {
        if (!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $params = $this->request->post();
        $model = new \app\admin\model\User();
        $user = $model->get($this->auth->id);
        if (!$user) {
            $this->error('用户信息不存在');
        }
        $wechatObj = (new \app\api\model\wddj\UserWechat())->where('user_id', $this->auth->id)->find();
        if (!$wechatObj) {
            $this->error('微信用户信息不存在');
        }

        $user['nickname'] = empty($params['nickname']) ? $user['nickname'] : $params['nickname'];
        $user['avatar'] = empty($params['avatar']) ? $user['avatar'] : $params['avatar'];
        $wechatObj->nickname = $user['nickname'];
        $wechatObj->avatar = $user['avatar'];
        $result = false;
        Db::startTrans();
        try {
            $result = $user->save();
            $wechatObj->save();
            Db::commit();
        } catch (ValidateException|PDOException|Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if (false === $result) {
            $this->error(__('No rows were updated'));
        }

        $this->success('更新成功！', $this->auth->getUserinfo());
    }
}



 